package org.zjt.rxjava.lession.second;

import io.reactivex.Observable;

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

/**
 * Description:
 *
 *   1、take 、skip 、 takeWhile 、skipWhile 、distinct 、elementAt 、cast 、scan（两两输出）、all （所有匹配）、any(有一个匹配)
 *
 * @author juntao.zhang
 * Date: 2018-06-28 下午4:30
 * @see
 */
public class BasicOperatorsTest {

    public static void main(String[] args) throws InterruptedException {
        Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
                .take(3)
                .subscribe(s -> System.out.println("take 3 个 RECEIVED 1: " + s));


        Observable.interval(300, TimeUnit.MILLISECONDS)
                .take(2, TimeUnit.SECONDS)
                .subscribe(i -> System.out.println("take 2 秒 RECEIVED 2: " + i));

        Observable.range(1,100)
                .skip(90)
                .subscribe(i -> System.out.println("划过前90个 RECEIVED 3: " + i));


        Observable.range(1,100)
                .takeWhile(i -> i < 5)
                .subscribe(i -> System.out.println("得到所有小于5的数值 RECEIVED 4 : " + i));


        Observable.range(1,100)
                .skipWhile(i -> i <= 95)
                .subscribe(i -> System.out.println("划过小于95的数值  RECEIVED 5: " + i));


        Observable.just("Alpha", "Beta", "Gamma", "Delta",
                "Epsilon")
                .map(String::length)
                .distinct()
                .subscribe(i -> System.out.println("distinct RECEIVED: " + i));


        Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon").distinct(String::length)
                .subscribe(i -> System.out.println("RECEIVED: " + i));


        Observable.just(1, 1, 1, 2, 2, 3, 3, 2, 1, 1)
                .distinctUntilChanged()
                .subscribe(i -> System.out.println("RECEIVED: " + i));


        Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon").elementAt(3).subscribe(i -> System.out.println("得到第三个元素 RECEIVED: " + i));


        Observable<Object> just = Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon");
        just.cast(String.class).subscribe(i -> System.out.println("cast 转化类型后 RECEIVED: " + i));


        Observable.just(5, 3, 7, 10, 2, 14).scan((accumulator, next) -> accumulator + next).subscribe(s -> System.out.println("scan Received: " + s));


        Observable.just(5, 3, 7, 10, 2, 14).all(i -> i > 10).subscribe(s -> System.out.println("Received: " + s));


        sleep(5000);
    }
}
